package com.decade.controller;

import com.decade.pojo.Goods;
import com.decade.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //查询
    @GetMapping({"/findGoods/{name}","/findGoods/"})
    public String findGoods(Goods goods, Model model){
       List<Goods> list = goodsService.findGoods(goods);
        model.addAttribute("list",list);
       return "goods_list";
    }

    //添加跳转
    @RequestMapping("toAddGoods")
    public String toAddGoods(Model model){
        return "goods_add";
    }

    //添加
    @PostMapping("/addGoods")
    public String addGoods(Goods goods,Model model){
       int row = goodsService.addGoods(goods);
       return "redirect:/goods/findGoods/";
    }

    //修改跳转
    @RequestMapping("/toUpdateGoods/{id}")
    public String tUpdateGoods(@PathVariable Integer id,Model model){
        Goods goods = goodsService.findGoodsById(id);
        model.addAttribute("goods",goods);
        return "goods_update";
    }

    //修改
    @PostMapping("/updateGoods")
    public String updateGoods(Goods goods,Model model){
        int row = goodsService.updateGoods(goods);
        return "redirect:/goods/findGoods/";
    }

    //删除
    @GetMapping("/delGoods/{id}")
    public String delGoods(@PathVariable(required = false) Integer id,Model model){
        int row = goodsService.delGoods(id);
        return "redirect:/goods/findGoods/";
    }
}
